import type { NvFormItemOption, NvFormOption } from '../types'
import { computed } from 'vue'

export function useGroup(props: { option: NvFormOption }) {
  const formGroupFields = computed(() => {
    console.debug('computed formGroupFields')
    const { group, fields } = props.option
    return group!.items.map((item) => {
      return item.fields.reduce((acc, field) => {
        if (fields[field]) {
          acc[field] = fields[field]
        }
        else {
          console.error(`[naive-crud]: field '${field}' is not defined`)
        }
        return acc
      }, {} as Record<string, NvFormItemOption>)
    })
  })

  return formGroupFields
}
